import { TXMapKey } from "./config";
const HTTP = "https://apis.map.qq.com/ws/geocoder/v1/";

export default {
	getLocation(clickHandler) {
		let that = this;
		uni.authorize({
			scope: "scope.userLocation",
			success() {
				// 允许授权
				that.getCurrentCoordition(clickHandler);
			},
			fail() {
				// 拒绝授权
				that.openConfirm();
			},
		});
	},
	// 当用户第一次拒绝后再次请求授权
	openConfirm() {
		const that = this;
		uni.showModal({
			title: "请求授权当前位置",
			content: "需要获取您的地理位置，请确认授权",
			success: (res) => {
				if (res.confirm) {
					uni.openSetting(); // 打开地图权限设置
				} else if (res.cancel) {
					uni.showToast({
						title: "你拒绝了授权，无法获得获取您的位置",
						icon: "none",
						duration: 1000,
					});
				}
			},
		});
	},
	//获取当前经纬度
	getCurrentCoordition(clickHandler) {
		uni.getFuzzyLocation({
			success: res => {
				console.log(res, "当前位置");
			},
			fail: res => {
				console.log(res, "失败原因");
			}
		});
	},
	//获取当前城市
	getCurrentAddress(lat, lng, clickHandler) {
		uni.request({
			url: HTTP + `?location=${encodeURIComponent(lat)},${encodeURIComponent(lng)}` + "&key=" + TXMapKey + "&get_poi=1",
			method: 'GET',
			success: res => {
				console.log("当前位置的地址");
				
				
				clickHandler & clickHandler("啊实打实大时代")
			}
		})
	}
};

/**
 * https://apis.map.qq.com/ws/geocoder/v1/
 * ?location=${encodeURIComponent(this.latitude)},${encodeURIComponent(this.longitude)}
 * &key=E7XBZ-FUXC7-D22XZ-POFT7-OD5LJ-6RBAV&get_poi=1
 */